"""
Case Type   : 共享存储-系统内部工具-dsscmd
Case Name   : dsscmd工具读取dss文件内容之-f format参数验证
Create At   : 2023/4/12
Owner       : opentestcase019
Description :
    1.读取dss文件内容，指定-f format参数为有效值
    2.读取dss文件内容，指定-f format参数为无效值
Expect      :
    1.成功
    2.合理报错
History     :
"""
import os
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common


class ShareStorageTool(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.primary_node = Node('PrimaryDbUser')
        self.common = Common()

    def test_sharestorage_tool(self):
        step1 = "-----step1:读取dss文件内容，指定-f format参数为有效值;expect:显示成功-----"
        self.logger.info(step1)
        self.logger.info("-----获取卷组名-----")
        execute_cmd = f"source {macro.DB_ENV_PATH};dsscmd lsvg"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn("Succeed to display lsvg info.", execute_msg, "获取卷组名失败")
        vgname = execute_msg.splitlines()[1].split()[0].strip()
        self.logger.info(vgname)
        cmd_list = ['-f c', '-f h', '-f u', '-f l', '-f s', '-f x']
        exp = f"filename is +{vgname}/pg_control, offset is 2048."
        for cmd in cmd_list:
            execute_cmd = f"source {macro.DB_ENV_PATH};" \
                f"dsscmd examine -p +{vgname}/pg_control -o 2048 {cmd}"
            self.logger.info(execute_cmd)
            execute_msg = self.common.get_sh_result(self.primary_node,
                                                    execute_cmd)
            self.assertIn(exp, execute_msg, "执行失败" + step1)

        step2 = "-----step2:读取dss文件内容，指定-f format参数为无效值;expect:合理报错-----"
        self.logger.info(step2)
        cmd_list = ['-f char', '-f unsigned', '-f short',
                    '-f int', '-f string', '-f hex']
        for cmd in cmd_list:
            execute_cmd = f"source {macro.DB_ENV_PATH};" \
                f"dsscmd examine -p +{vgname}/pg_control -o 2048 {cmd}"
            self.logger.info(execute_cmd)
            execute_msg = self.common.get_sh_result(self.primary_node,
                                                    execute_cmd)
            self.assertIn("The name's letter of format should be "
                          "[c|h|u|l|s|x].", execute_msg, "执行失败" + step2)

    def tearDown(self):
        self.logger.info("-----无需环境清理-----")
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
